package org.edu.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.edu.bean.TmBizopen;
import org.edu.bean.TmOpenType;
import org.edu.common.PageView;
import org.edu.common.QueryResult;
import org.edu.service.TmBizopenService;

import com.opensymphony.xwork2.ActionSupport;
/**
 * 开业登记
 * @author lzh
 *
 * 2012-4-9
 */
@SuppressWarnings("serial")
public class TmBizOpenController extends ActionSupport {
	@Resource
	private TmBizopenService tmBizopenService;
	private TmBizopen entity;
	private Integer id;
	private Integer page = 1;
	private String isQuery;
	private String condition;
	private String opentype;
	
	public String getOpentype() {
		return opentype;
	}

	public void setOpentype(String opentype) {
		this.opentype = opentype;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String getIsQuery() {
		return isQuery;
	}

	public void setIsQuery(String isQuery) {
		this.isQuery = isQuery;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		if(page == null || page == 0)
			this.page = 1;
		else
			this.page = page;
	}

	public TmBizopen getEntity() {
		return entity;
	}

	public void setEntity(TmBizopen entity) {
		this.entity = entity;
	}
	public String input() throws Exception {
		return INPUT;
	}
	public String list() throws Exception {
		PageView<TmBizopen> pageView = new PageView<TmBizopen>(20,page);
		QueryResult<TmBizopen> queryResult = null;
		StringBuilder sb = new StringBuilder("0=0 ");
		List<Object> params = new ArrayList<Object>();
		if(opentype != null && !"".equals(opentype.trim())){
			sb.append("AND o.status=? ");
			params.add(TmOpenType.valueOf(opentype.trim()));
		}
		if("yes".equals(isQuery)){
			if(condition != null && !"".equals(condition.trim())) {
				sb.append("AND o.regno=? OR o.taxplayer LIKE ?");
				params.add(condition.trim());
				params.add("%"+condition.trim()+"%");
				queryResult = tmBizopenService.getQueryResult(pageView.getFirstResult()
						,pageView.getMaxresult(),sb.toString()
						,params.toArray());
			}
		}else{
			queryResult = tmBizopenService.getQueryResult(pageView.getFirstResult()
					,pageView.getMaxresult(),sb.toString(),params.toArray());
		}
		pageView.setQueryResult(queryResult);
		ServletActionContext.getRequest().setAttribute("pageView",pageView);
		ServletActionContext.getRequest().setAttribute("opentype",opentype);
		return "show";
	}
	public String update() throws Exception {
		try {
			tmBizopenService.save(entity);
			addActionMessage("变更【"+entity.getRegno()+"】成功");
		} catch (Exception e) {
			addActionMessage("变更【"+entity.getRegno()+"】失败");
			e.printStackTrace();
		}
		return "message";
	}
	public String findById() throws Exception {
		TmBizopen obj = tmBizopenService.find(id);
		if(obj != null && obj.getStatus().equals(TmOpenType.OPEN)) {
			this.addActionMessage(obj.getRegno() + "已经通过了审核无法变更");
			return SUCCESS;
		}
		ServletActionContext.getRequest().setAttribute("entity",obj);
		return "update";
	
	}
	@Override
	public String execute() throws Exception {
		try {
			tmBizopenService.save(entity);
			this.addActionMessage("开业登记【"+entity.getRegno()+"】成功");
		} catch (Exception e) {
			this.addActionMessage("开业登记【"+entity.getRegno()+"】失败");
			e.printStackTrace();
		}		
		return "message";
	}
}
